var currentObj;
jQuery(function($){
	/* old version */
	$('#all').toggle(
		function(){
			tree_init('open');
			tree_menu.find('.onecontrol').addClass('onecontrol_on');
			tree_menu.find('.twocontrol').addClass('twocontrol_on');
			return false;
		},
		function(){
			tree_init('close');
			tree_menu.find('.onecontrol').removeClass('onecontrol_on');
			tree_menu.find('.twocontrol').removeClass('twocontrol_on');
			return false;
		}
	);
	$('.admin_edit .tab a').click(function(){
		$('.tab li').removeClass('on');
		$(this).parent('li').addClass('on');	
		if ($('.tab li:first').attr('class') == 'first on'){
			$('.admin_edit div').hide(); $('.admin_edit div:first').show();
		} else if ($('.tab li:last').attr('class') == 'on'){
			$('.admin_edit div').hide(); $('.admin_edit div:last').show();
		}
		return false;	
	});
	$('.admin_edit .tab a').click(function(){
		$('.tab li').removeClass('on');
		$(this).parent('li').addClass('on');	
		if ($('.tab li.firsttab').attr('class') == 'firsttab on'){
			$('.admin_edit .editarea').hide(); $('.admin_edit .edittxt').show();
		} else if ($('.tab li.twotab').attr('class') == 'twotab on'){
			$('.admin_edit .editarea').hide(); $('.admin_edit .textareatxt').show();
		}
		return false;	
	});
	$('.list_ex_step dd:not(:first)').css('display','none'); 
	$('.list_ex_step dt:first').addClass('selected');
	$('.list_ex_step .next').click(function(){
		var selval =  $(this).parent().prev().find(".radio:checked").next("label").find("img").attr("alt");
		$(this).parent().parent().prev().find('.select_val').text(selval);
		if($(this).parent().parent().next().next().css('display') == 'none') {
			$('dd').slideUp('slow');
			$(this).parent().parent().next().next().slideDown('slow');
			$('dt').removeClass('selected');
			$(this).parent().parent().next().addClass('selected');
		}
		return false;
	});
	$('.list_ex_step .prev').click(function(){
		$(this).parent().parent().prev().prev().prev().children('.select_val').text('');
		if($(this).parent().parent().prev().prev().css('display') == 'none') {
			$('dd').slideUp('slow');
			$(this).parent().parent().prev().prev().slideDown('slow');
			$('dt').removeClass('selected');
			$(this).parent().parent().prev().prev().prev().addClass('selected');
		}
		return false;
	});
	$('.list_ex_step .reload').click(function(){
		$('.list_ex_step dd:not(:first)').slideUp('slow');
		$('.list_ex_step dt:not(:first)').removeClass('selected');
		$('.list_ex_step dd:first').slideDown('slow');
		$('.list_ex_step dt:first').addClass('selected');
		$('.list_ex_step .select_val').text('');
		return false;
	});
	$('.list_ex_question li').hover(
		function(){
			$(this).children().children('.box_ex').addClass('box_ex_on');
		},
		function(){
			$(this).children().children('.box_ex').removeClass('box_ex_on');
		}
	);
	/* //old version */
	/* added */

	/* newLnbType */
	var newLnbType = {
		init : function() {
			this.addMenuClass();
			this.checkCurrent(currentObj);
		},
		addMenuClass : function() {
			var menuWrap = $("div#lnb"),
				menu1Depth = menuWrap.children("ul"),
				menu2Depth = menuWrap.find("li > ul");
			// old version reset
			if ( currentObj !== undefined ) {
				menuWrap
					.find("ul, li, a")
						.removeClass();
			}
			menu1Depth
				.find("> li > a")
					.addClass("onecontrol nosub")
				.end()
				.find("li:last")
					.addClass("end");
			menu2Depth
				.addClass("threedepth")
				.siblings("a")
				.removeClass("nosub");
			newLnbType.toggleMenu(menuWrap, menu1Depth, menu2Depth);
		},
		toggleMenu : function(menuWrap, menu1Depth, menu2Depth) {
			var tgAnchor = menu1Depth.children("li").find("> a:not('.nosub')");
			menu2Depth.hide();
			tgAnchor.on("click", function(e) {
				var $this = $(this),
					tg2Depth = $this.siblings("ul.threedepth");
				e.preventDefault();
				menu2Depth.not(tg2Depth).slideUp("fast");
				tg2Depth
					.slideDown("fast");
			});
		},
		checkCurrent : function(currentObj) {
			var menuWrap = $("div#lnb"),
				tgAnchor = menuWrap.find("a[id*='" + currentObj + "'], a.currentMenu");
			if ( currentObj !== undefined || tgAnchor.length !== 0 ) {
				if ( tgAnchor.parents("ul.threedepth").length !== 0 ) {
					tgAnchor
						.parent("li")
							.addClass("sub_on1")
						.closest("ul.threedepth")
							.show()
						.closest("li")
							.addClass("now_lnbm");
				} else {
					tgAnchor.parent("li").addClass("now_lnbm");
				}
			} else {
				menuWrap.find("ul").show();
			}
		}
	};
	newLnbType.init();
	/* //newLnbType */

	// modify style old pagination
	$("div.paging").find("span a").last().css("background","none");

	// main
	if ( $("div#wrapper").find("div.mainTopAreaBoxTop").length > 0 ) {
		$("div.mainTopAreaBoxBottom.mTopBox3.nTabUi").tabs({event:"mouseover"});
		$("div.mainMidAreaWrap").wrap("<div style='border-top:1px solid #d3d3d3;'></div>");
		$("div#wrapper").addClass("mContainerBg")
	};

	/* ie6 */
	if ( $.browser.msie && parseInt($.browser.version, 10) < 7 ) {
		$(".footerMenu .privacyMenu li:first-child").addClass("firstChild");
	};
	/* //ie6 */

	/* gnb */

	/* rOverImgGnb */
	var rOverImgGnb = {
		init : function() {
			this.checkImg();
		},
		checkImg : function() {
			var tgImg = $("#header ul:last li img");
			tgImg.each(function() {
				var $this = $(this),
					tgSrc = $this.attr("src"),
					onSrc = $this.attr("src").replace(".gif","_on.gif"),
					offSrc = $this.attr("src").replace("_on.gif",".gif");
				$this.hover(
					function() {
						$this.attr("src",onSrc);
					},
					function() {
						$this.attr("src",offSrc);
					}
				);
			});
		}
	}
	rOverImgGnb.init();
	/* //rOverImgGnb */	

	/* depthLayer */
	var depthLayer = {
		init : function() {
			this.toggleLayer();
		},
		toggleLayer : function() {
			var tgGnb = $("div#header > ul").last().find("> li"),
				depthLayer = $("div.depthLayer").filter("[class*='Depth']"),
				outOfLayer = $("div#container");
			tgGnb.on("mouseenter", function() {
				var $this = $(this),
					thisIndex = $this.index() + 1;
				if ( depthLayer.filter(":visible").length !== 1 ) {
					depthLayer
						.filter("[class*='gnb" + thisIndex + "Depth']")
							.slideDown("fast");
				} else {
					depthLayer
						.filter("[class*='gnb" + thisIndex + "Depth']")
							.show();
				}
				depthLayer
					.parents("div#head")
					.find(depthLayer)
						.not("[class*='gnb" + thisIndex + "Depth']")
						.hide()
					.end()
					.on("mouseleave", function() {
						depthLayer.slideUp("fast");	
					});
			});
		}
	}
	depthLayer.init();
	/* //depthLayer */

	/* calcRange */
	var calcRange = {
		init : function() {
			var sliderRangeMin = $("body").find("div.sliderRangeMin");
			if ( sliderRangeMin.length !== 0 ) {
				this.setDefaultVal();
				this.calcRange1();
				this.calcRange2();
				this.calcRange3();
			}
		},
		setDefaultVal : function() {
			var valInput = $("input[class*='Value']");
			valInput.focusout(function() {
				if ( $(this).val() === "" ) {
					$(this).val("0");
				}
			});
		},
		calcRange1 : function() {
			var calcRange1 = $("body").find("div.calcRange1"),
				valInput = $("input.calcRange1Value"),
				valBtn = $("div.calcItem.calc2 a"),
				rangeValMax = 100000000,
				overMaxValAlert = "최대 대출금액은 1억원 입니다.";
			calcRange1.slider({
				range: "min",
				value: 0,
				min: 0,
				max: rangeValMax,
				step: 1000000,
				slide: function(event,range1) {
					valInput.val(range1.value);
					calcRange.setNumComma(valInput);
				}
			});
			valBtn
				.not(":last-child")
					.on("click", function(e) {
						var $this = $(this),
							index = $this.index(),
							valNum = parseInt(valInput.val().replace(/,/g,''));
						e.preventDefault();
						if ( index === 0 && valNum + 1000000 <= rangeValMax) {
							var plusVal = 1000000;
						} else if ( index === 1 && valNum + 5000000 <= rangeValMax) {
							var plusVal = 5000000;
						} else if ( index === 2 && valNum + 10000000 <= rangeValMax) {
							var plusVal = 10000000;
						} else {
							var plusVal = 0;
							alert(overMaxValAlert);
							return false;
						}
						calcRange1.slider("value",valNum + plusVal);
						valInput.val(valNum + plusVal);
						calcRange.setNumComma(valInput);
					})
				.end()
				.filter(":last-child")
					.on("click", function(e) {
						e.preventDefault();
						calcRange1.slider("value",0);
						valInput.val("0");
					});
			valInput
				.val(calcRange1.slider("value"))
				.keyup(function() {
					calcRange.setNumComma($(this));
					calcRange1.slider("value",$(this).val().replace(/,/g,''));
				})
				.focusout(function() {
					if ( valInput.val().replace(/,/g,'') > rangeValMax ) {
						alert(overMaxValAlert);
						$(this).val(rangeValMax);
						calcRange.setNumComma($(this));
					}
				});
		},
		setNumComma : function(tgInputTxt) {
			var valNum = tgInputTxt.val().replace(/,/g,'');
			tgInputTxt.val(valNum.replace(/(\d)(?=(\d{3})+(?!\d))/g,"$1,"));
		},
		calcRange2 : function() {
			var calcRange2 = $("body").find("div.calcRange2"),
				valInput = $("input.calcRange2Value"),
				valBtn = $("div.calcItem.calc3 a"),
				rangeValMax = 60,
				overMaxValAlert = "최대 대출기간은 60개월 입니다.";
			calcRange2.slider({
				range: "min",
				value: 0,
				min: 0,
				max: rangeValMax,
				step: 1,
				slide: function(event,ui) {
					valInput.val(ui.value);
				}
			});
			valBtn
				.not(":last-child")
					.on("click", function(e) {
						var $this = $(this),
							index = $this.index();
						e.preventDefault();
						if ( index === 0 && parseInt(valInput.val()) + 1 <= rangeValMax) {
							var plusVal = 1;
						} else if ( index === 1 && parseInt(valInput.val()) + 6 <= rangeValMax) {
							var plusVal = 6;
						} else if ( index === 2 && parseInt(valInput.val()) + 12 <= rangeValMax) {
							var plusVal = 12;
						} else if ( index === 3 && parseInt(valInput.val()) + 36 <= rangeValMax) {
							var plusVal = 36;
						} else {
							var plusVal = 0;
							alert(overMaxValAlert);
							return false;
						}
						calcRange2.slider("value",parseInt(valInput.val()) + plusVal);
						valInput.val( parseInt(valInput.val()) + plusVal );
					})
				.end()
				.filter(":last-child")
					.on("click", function(e) {
						e.preventDefault();
						calcRange2.slider("value",0);
						valInput.val("0");
					});
			valInput
				.val(calcRange2.slider("value"))
				.keyup(function() { 
					calcRange2.slider("value",$(this).val());
				})
				.focusout(function() {
					if ( valInput.val() > rangeValMax ) {
						alert(overMaxValAlert);
						$(this).val(rangeValMax);
					}
				});
		},
		calcRange3 : function() {
			var calcRange3 = $("body").find("div.calcRange3"),
				valInput = $("input.calcRange3Value"),
				rangeValMax = 100,
				overMaxValAlert = "최대 대출이율은 100% 입니다.";
			calcRange3.slider({
				range: "min",
				value: 0,
				min: 0,
				max: rangeValMax,
				step: 0.1,
				slide: function(event,ui) {
					valInput.val(ui.value);
				}
			});
			valInput
				.val(calcRange3.slider("value"))
				.keyup(function() {
					calcRange3.slider("value",$(this).val());
			});
			valInput.focusout(function() {
				if ( valInput.val() > rangeValMax ) {
					alert(overMaxValAlert);
					$(this).val(rangeValMax);
				}
			});
		}
	};
	calcRange.init();
	/* //calcRange */
		
	/* makeNumSpan */
	var makeNumSpan = {
		init : function() {
			if ( $("div.detailNum").length > 0 ) this.checkNum();
		},
		checkNum : function() {
			var tgNum = $("div.detailNum em");
			tgNum.each(function() {
				var $this = $(this),
					thisNum = $this.text(),
					thisNumEach = thisNum.split(""),
					thisNumLength = thisNumEach.length;
				// make span
				$.each(thisNumEach,function(index,number) {
					if ( number === "." ) var number = "Dot";
					$this.before("<span class='numSpan numBg" + number + "'></span>");
					$("div.detailNum span").not("[class]").css("margin-left","-2px");
				});
			});
		}
	};
	makeNumSpan.init();
	/* //makeNumSpan */

	/* familySiteArea */
	var familySiteArea  = {
		init : function() {
			this.toggleList();
		},
		toggleList : function() {
			var tgBtn = $("a.familySiteBtn"),
				tgList = tgBtn.next("ul");
			tgBtn.hover(function() {
				tgList
					.fadeIn("fast")
					.position({
						of : tgBtn,
						offset : "0 1",
						at : "center top",
						my : "center bottom"
					});
			});
			$(".footerMenu").mouseleave(function() {
				tgList.fadeOut("fast");
			});
			tgList.mouseleave(function() {
				tgList.fadeOut("fast");
			});
		}
	};
	familySiteArea.init();
	/* //familySiteArea */

	/* footerHeight */
	var footerHeight = {
		init : function() {
			this.checkHeight();
		},
		checkHeight : function() {
			var body = $("body"),
				windowHeight = $(window).height(),
				bodyHeight = body.height(),
				footerMenu = $("div.footerMenu"),
				footerMenuHeight = footerMenu.height();
			if ( windowHeight > bodyHeight ) {
				( $.browser.msie )
					? footerMenu
						.height(footerMenu.outerHeight() + (windowHeight - bodyHeight	) - 18)
					: footerMenu
						.height(footerMenu.outerHeight() + (windowHeight - bodyHeight	) );
			}
		}
	};
	footerHeight.init();
	/* //footerHeight */
	
	/* tbHeaderTableWrap */
	var tbHeaderTableWrap = {
		init : function() {
			var tbHeaderTable = $("table.tbHeaderTable");
			if ( tbHeaderTable.length > 0 ) {
				this.fixit(tbHeaderTable);
			}
		},
		fixit : function(tbHeaderTable) {
			tbHeaderTable.position({
				of : ".tbWrapStyle",
				my : "left top",
				at : "left top"
			});
			$("div.tbWrapStyle").scroll(function() {
				tbHeaderTable.position({
					of : ".tbWrapStyle",
					my : "left top",
					at : "left top"
				});
			});
			if ( $.browser.msie && parseInt($.browser.version, 10) < 8 ) {
				tbHeaderTable.position({offset:"1 0"});
				$("div.tbWrapStyle").scroll(function() {
					tbHeaderTable.position({offset:"1 0"});
				});
			};
		}
	};
	tbHeaderTableWrap.init();
	/* tbHeaderTableWrap */

	// agreeArea
	var agreeArea = {
		init : function() {
			var agree = $("div").filter(".agree");
			if ( agree.length > 0 ) this.addClass(agree);
		},
		addClass : function(agree) {
			var tgList = agree.find("li");
			tgList
				.filter(":first-child, :nth-child(2)")
				.addClass("firstItem")
					.end()
				.filter(":nth-child(2n)")
					.addClass("rightItem")
				.end()
				.filter(":last-child")
					.addClass("LastItem")
				.end()
					.filter(".LastItem")
					.prev()
						.addClass("LastItem");
			if ( tgList.length%2 !== 0 && tgList.length !== 1 ) {
				tgList
					.last()
					.after("<li class='rightItem' />");
			}
		}
	};
	agreeArea.init();
	
	// visualArea(main)
	var visualArea = {
		init: function() {
			var tgArea = $("div[class*='visualArea']");
			if ( tgArea.length > 0 ) this.tgSlide(tgArea);
		},
		tgSlide : function(tgArea) {
			var tgSlide = tgArea.find("div.loanMainSlide"),
				slideArea = tgArea.find("div.slideArea");
			tgSlide
				.scrollable({
					items : ".items",
					circular : true
				})
				.navigator({
					navi : ".slideBtn",
					activeClass : "current"
				});
			// title
			var slideBtn = tgArea.find("div.slideBtn a"),
				tgSlideData = tgSlide.data("scrollable");
			// auto scroll
			if ( tgSlideData && tgSlideData.getSize() > 1 ) {
				tgSlide
					.autoscroll({
						autoplay: true,
						interval : 5000
					});				
			}
			slideBtn
				.each(function() {
					var $this = $(this),
						titTxt = slideArea.eq($this.index()).find("h2 span").text();
					$this.html("<span>" + titTxt + "</span>");
				})
				.on("mouseenter", function() {
					var $this = $(this);
					if ( !$(this).hasClass("current") ) {
						slideBtn.removeClass("current");
						$this
							.addClass("current")
							.find("span")
							.show("slide");
					}
				})
				.closest(".slideBtn")
				.on("mouseleave", function() {
					var $this = $(this);
					if ( slideBtn.index( $this.find(".current") ) != tgSlideData.getIndex() ) {
						slideBtn
							.removeClass("current")
							.eq(tgSlideData.getIndex())
								.addClass("current")
								.find("span")
								.show("slide");
					};
				});
				slideArea.each(function() {
					var $this = $(this), 
						maxLimit = $this.find(".detailNum em:eq(1)").text();
						$this.find(".visualTxt02").find("span").text(maxLimit);
				});
		}
	};
	visualArea.init();
	/* //added */

	/* 전체메뉴보기(by.miran) */
	// 활성화메뉴 있을경우
	$(".allMenuBtn").bind("mouseover click", function(){
		$(".allMenu").css("display","block");
		return false;
	});
	$(".allMenu, div#wrapper").on("mouseleave", function(){
		$(".allMenu").css("display","none");
	});
	$("div#container").on("mouseenter", function(){
		$(".allMenu").css("display","none");
	});
	if($(".main_menu a").is(".current") ){
		var $curr = $(".main_menu").find(".current");
		var $menu1 = $curr.parent().parent().parent().find(">a>img");
		var $menu2 = $curr.parent().parent().parent().parent().parent().find(">a>img");
		$curr.parent().parent().css("display","block");
		$curr.parent().parent().parent().parent().css("display","block");
		$menu1.attr("src",$menu1.attr("src").replace("bu_1depth_close","bu_1depth_open"));
		$menu1.attr("src",$menu1.attr("src").replace("bu_2depth_close","bu_2depth_open"));
		$menu2.attr("src",$menu2.attr("src").replace("bu_1depth_close","bu_1depth_open"));
	}
	// 1depth click
	var obgMenu = $(".main_menu>li");
	var objMenu2 = $(".sub_menu>li");
	var objSubMenu = obgMenu.find(".sub_menu");
	$(".main_menu>li>a").each(function(index){
		$(this).click(function(){
			var objMenuIndex = obgMenu.eq(index);
			var objIndexSub = objMenuIndex.find(".sub_menu");
			if (objIndexSub.css('display') == 'none'){
				objSubMenu.css('display', 'none'); // 지우면 펼쳐짐상태 유지됨
				objIndexSub.css('display', 'block');
				// 하위메뉴 있을때 블릿
				if (obgMenu.find(".sub_menu")){
					objMenuIndex.find("a>img:eq(0)").attr("src",$(this).parent().find("img").attr("src").replace("bu_1depth_close","bu_1depth_open"));
				}
			}else{
				objIndexSub.css('display', 'none');
				$(this).find("img").attr("src",$(this).parent().find("img").attr("src").replace("bu_1depth_open","bu_1depth_close"));
				//location.href=$(this).attr("href");
			}
			//return false;
		});
	});
	// 2depth click
	$(".sub_menu>li>a").click(function(){
		//location.href=$(this).attr("href");
	});
	/* //전체메뉴보기(by.miran) */
});